
//
// 201220 - AH - (Pygmalion) Created.
//

package org.soekrat.gui.swing.conversationelement;

import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.event.SoekratChangeEvent;
import org.soekrat.engine.event.SoekratChangeListener;
import org.soekrat.engine.motor.ConversationEvent;
import org.soekrat.engine.orb.Soekrat2Orb;

/**
 * <p>
 *   Panel that shows 
 *   {@link ConversationEvent}s.
 * </p>
 * @author Arne Halvorsen (AH) 
 */
@SuppressWarnings("serial")
public final class ConversationEventsPanel extends JPanel
{
  private DefaultListModel m_model=new DefaultListModel();
  
  private JList m_list=new JList(m_model);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ConversationEventsPanel(Soekrat2Engine engine)
  {
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    final Soekrat2Orb ce=
      (Soekrat2Orb)engine.getAttribute("conversation-element");
    
    ce.addSoekratChangeListener
    (
      new SoekratChangeListener() 
      {
        public void changed(SoekratChangeEvent e)
        { 
          setHistory(ce);
        }
      }
    );
    
    setLayout(new BorderLayout());
    
    add(new JScrollPane(m_list), BorderLayout.CENTER);  
  }
  
  /**
   * <p>
   *   Adds 
   *   {@link ConversationEvent} history of given
   *   {@link Soekrat2Orb}.
   * </p>
   * @param ce {@code ConversationElement}.
   */
  public void setHistory(Soekrat2Orb ce)
  {
    if (ce==null)
    {
      throw new NullPointerException("ce");
    }
    
    m_model.removeAllElements();
    
    int n=ce.getEventCount();
    
    for (int i=0; i<n; i++)
    {
      ConversationEvent e=ce.getConversationEvent(i);
      m_model.addElement(e);
    }
  }
  
}
